package cn.echo.modules.sys.controller.gradebook;



import cn.echo.modules.sys.entity.Grade;
import cn.echo.modules.sys.entity.Guest;
import cn.echo.modules.sys.service.GradeService;
import cn.echo.modules.sys.service.GuestService;
import cn.echo.modules.sys.service.SiteService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by 简约 鑫 on 2017/4/14.
 */
@Controller//成为组件让系统可以注入这个对象
@RequestMapping("/Guest")
public class GuestController {
    //    @Autowired//按照类型注入(装配)--自动找到实现了GuestService类
//    @Qualifier("service")
    @Autowired
    private GuestService guestService;
    //1:@Autowire 按照类型自动装配(接口找实现的类) 如果是类就找这个类
    //2:@Autowired@Qualifier("service")通过名字装配 保证@Qualifier("名字")和@Service("名字")名字相同
    //3:@Resource 先通过名字找找不到通过类型找
    //4:基本数据类型可以用@Value("")
    //控制反转的注解:
    //@Component @Controller @Service @Repository
    //依赖注入的注解:
    //@Autowire @Qualifier  @Resource @Value
//    @Scope("prototype") 在类配置对象的域 单例 多例 request session
//    初始化：@PostConstruct
//    销毁：@PreDestroy
//            = new SiteService();
   @RequestMapping(value ={"guest",""},method = RequestMethod.GET)
    public String showResult(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                             @RequestParam(required = false,defaultValue = "5") Integer pageSize,
                             Model model, HttpServletRequest req, @RequestParam(required = false)String message){
//        pageNum = Integer.valueOf(req.getParameter("pageNum"));
//        pageSize = Integer.valueOf(req.getParameter("pageSize"));
        //赋值页面页码大小自动帮我们计算，我们只需要在页面上使用PageInfo的数据就行
        PageHelper.startPage(pageNum,pageSize);
        Page<Guest> guestList = (Page) guestService.findAll(new Guest());
        //分页信息
        PageInfo<Guest> pageInfo = new PageInfo(guestList);
        req.setAttribute("page",pageInfo);
       return "sys/guestbook/Guest";
    }

    @RequestMapping(value ="guestDelete",method = RequestMethod.GET)
    public String deleteById(Guest guest){
        guestService.deleteById(guest);
        return "redirect:guest";
    }

   }
